package persistence;

import java.util.List;

import javax.persistence.EntityManager;

import model.ContactGroup;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 
 * @author Justin Templemore
 * @author hbriand (modified version for this project)
 * 
 * @class ContactGroupDAO
 * @desc Maps a ContactGroup with its table in DB
 */
public class ContactGroupDAO
{
	private EntityManager currentEM;
	
	protected static Log log = LogFactory.getLog(ContactGroupDAO.class);
	
	public ContactGroupDAO ()
	{
		this.currentEM = PersistenceUtil.getEM();
	}
	protected EntityManager getEM()
	{
		return currentEM;
	}
	public void persist (ContactGroup cg)
	{
		currentEM.persist(cg);
	}
	public void remove (ContactGroup cg)
	{
		currentEM.remove(cg);
	}
	public ContactGroup merge (ContactGroup cg)
	{
		return currentEM.merge (cg);
	}
	public void refresh (ContactGroup cg)
	{
		currentEM.refresh (cg);
	}
	@SuppressWarnings("unchecked")
	public List<ContactGroup> findSome (int first, int max)
	{
		return (List<ContactGroup>) currentEM.createQuery("from ContactGroup as cg").setFirstResult(first).setMaxResults(max).getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<ContactGroup> findAll ()
	{
		return (List<ContactGroup>) currentEM.createQuery("from ContactGroup as cg").getResultList();
	}
	public long count ()
	{
		return ((Long) currentEM.createQuery("select count(p) from ContactGroup cg").getSingleResult()).longValue();
	}

    public void close() {
        this.currentEM.close();
    }
	public ContactGroup findById(long cgid) {
		return (ContactGroup)currentEM.createQuery("from ContactGroup as cg WHERE id="+cgid).getSingleResult();
	}
}